package com.slipper.common.util;

import java.text.FieldPosition;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CustomDateFormat extends SimpleDateFormat {
    private static final long serialVersionUID = 1L;
    
    @Override
    public StringBuffer format(
            Date date,
            StringBuffer toAppendTo,
            FieldPosition fieldPosition) {
        if (date instanceof java.sql.Date) {
            return toAppendTo.append(DateUtil.formatDate(date));
        } else {
            return toAppendTo.append(DateUtil.formatTime(date));
        }
    }
    
    @Override
    public Date parse(String source) throws ParseException {
        if (source != null && !source.isEmpty()) {
            return DateUtil.parseDate(source);
        } else {
            return null;
        }
    }
}
